<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户电话" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入客户电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="星级" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择星级" clearable>
          <el-option
            v-for="dict in dict.type.level_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="贷款类型" prop="loanType">
        <el-select v-model="queryParams.loanType" placeholder="请选择贷款类型" clearable>
          <el-option
            v-for="dict in dict.type.loan_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入年龄"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in dict.type.sex_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学历 " prop="education">
        <el-input
          v-model="queryParams.education"
          placeholder="请输入学历"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有房" prop="house">
        <el-select v-model="queryParams.house" placeholder="请选择是否有房" clearable>
          <el-option
            v-for="dict in dict.type.house_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否有车" prop="car">
        <el-select v-model="queryParams.car" placeholder="请选择是否有车" clearable>
          <el-option
            v-for="dict in dict.type.car_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="保单" prop="warranty">
        <el-select v-model="queryParams.warranty" placeholder="请选择保单" clearable>
          <el-option
            v-for="dict in dict.type.warranty_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="社保" prop="insurance">
        <el-select v-model="queryParams.insurance" placeholder="请选择社保" clearable>
          <el-option
            v-for="dict in dict.type.insurance_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="公积金" prop="fund">
        <el-select v-model="queryParams.fund" placeholder="请选择公积金" clearable>
          <el-option
            v-for="dict in dict.type.fund_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工资" prop="salary">
        <el-input
          v-model="queryParams.salary"
          placeholder="请输入工资"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请额度" prop="applyMoney">
        <el-select v-model="queryParams.applyMoney" placeholder="请选择申请额度" clearable>
          <el-option
            v-for="dict in dict.type.quota_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-select v-model="queryParams.province" @clear="clearSelect('1','s')" placeholder="请选择省份" clearable>
          <el-option
            @click.native="getChildren(p.code,'1','s')"
            v-for="p in this.provinceList"
            :key="p.code"
            :label="p.name"
            :value="p.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="城市" prop="city">
        <el-select v-model="queryParams.city" placeholder="请选择城市" @clear="clearSelect('2','s')" clearable>
          <el-option
            @click.native="getChildren(p.code,'2','s')"
            v-for="p in this.searchCityList"
            :key="p.code"
            :label="p.name"
            :value="p.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="区县" prop="county">
        <el-select v-model="queryParams.county" placeholder="请选择区县" clearable>
          <el-option
            v-for="p in this.searchCountyList"
            :key="p.code"
            :label="p.name"
            :value="p.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户经理" prop="manager">
        <el-input
          v-model="queryParams.manager"
          placeholder="请输入客户经理"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="客户状态" prop="customerStatus">-->
      <!--        <el-select v-model="queryParams.customerStatus" placeholder="请选择客户状态" clearable>-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.customer_status"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="创建时间" prop="createDate">
        <el-date-picker clearable
                        v-model="queryParams.createDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['customer:myCustomer:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['customer:myCustomer:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handlePull"
          v-hasPermi="['customer:myCustomer:deptPull']"
        >拉取
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['customer:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['customer:myCustomer:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="id" v-if="false"/>
      <el-table-column label="姓名" align="center" prop="name" width="100px" fixed="left"/>
      <el-table-column label="客户电话" align="center" prop="tel" width="120px" fixed="left"/>
      <el-table-column label="所属部门" align="center" prop="deptName" width="120px" fixed="left"
                       show-overflow-tooltip/>
      <el-table-column label="上级部门" align="center" prop="parentDeptName" width="120px" fixed="left"
                       show-overflow-tooltip/>
      <el-table-column label="星级" align="center" prop="level" :fixed="'left'">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.level_type" :value="scope.row.level"/>
        </template>
      </el-table-column>
      <el-table-column label="贷款类型" align="center" prop="loanType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.loan_type" :value="scope.row.loanType"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sex_type" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="标签" align="center" prop="label"/>
      <el-table-column label="学历" align="center" prop="education"/>
      <el-table-column label="是否有房" align="center" prop="house">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.house_type" :value="scope.row.house"/>
        </template>
      </el-table-column>
      <el-table-column label="是否有车" align="center" prop="car">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.car_type" :value="scope.row.car"/>
        </template>
      </el-table-column>
      <el-table-column label="保单" align="center" prop="warranty">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.warranty_type" :value="scope.row.warranty"/>
        </template>
      </el-table-column>
      <el-table-column label="社保" align="center" prop="insurance">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.insurance_type" :value="scope.row.insurance"/>
        </template>
      </el-table-column>
      <el-table-column label="公积金" align="center" prop="fund">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fund_type" :value="scope.row.fund"/>
        </template>
      </el-table-column>
      <el-table-column label="工资" align="center" prop="salary"/>
      <el-table-column label="申请额度" align="center" prop="applyMoney">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.quota_type" :value="scope.row.applyMoney"/>
        </template>
      </el-table-column>
      <el-table-column label="省份" align="center" prop="province"/>
      <el-table-column label="城市" align="center" prop="city"/>
      <el-table-column label="区县" align="center" prop="county"/>
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="客户经理" align="center" prop="manager"/>
      <el-table-column label="客户状态" align="center" prop="customerStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_status" :value="scope.row.customerStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
      </el-table-column>
      <el-table-column label="备注" align="center" prop="bz" width="200px">
        <template slot-scope="scope">
          <div class="messageCard" v-html="scope.row.bz">
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['customer:myCustomer:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="bathDistributionCustomer(scope.row)"
            v-hasPermi="['customer:myCustomer:allot']"
          >分配
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="customerReturn(scope.row)"
            v-if="scope.row.parentDeptId != 0"
            v-hasPermi="['customer:myCustomer:return']"
          >退回
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['customer:myCustomer:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="客户电话" prop="tel">
          <el-input v-model="form.tel" maxlength="11" :disabled="isEdit" placeholder="请输入客户电话"/>
        </el-form-item>
        <el-form-item label="所属部门" prop="distributeDeptId" v-if="isEdit">
          <el-select v-model="form.distributeDeptId" placeholder="请选择归属部门" :disabled="isEdit">
            <el-option
              v-for="dict in form.id == null ? this.topDeptList : this.allDeptList"
              :key="dict.deptId"
              :label="dict.deptName"
              :value="dict.deptId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="星级" prop="level">
          <el-select v-model="form.level" placeholder="请选择星级">
            <el-option
              v-for="dict in dict.type.level_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="贷款类型" prop="loanType">
          <el-select v-model="form.loanType" placeholder="请选择贷款类型">
            <el-option
              v-for="dict in dict.type.loan_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄"/>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sex_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签" prop="label">
          <el-input v-model="form.label" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="学历 " prop="education">
          <el-input v-model="form.education" placeholder="请输入学历 "/>
        </el-form-item>
        <el-form-item label="是否有房" prop="house">
          <el-select v-model="form.house" placeholder="请选择是否有房">
            <el-option
              v-for="dict in dict.type.house_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否有车" prop="car">
          <el-select v-model="form.car" placeholder="请选择是否有车">
            <el-option
              v-for="dict in dict.type.car_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="保单" prop="warranty">
          <el-select v-model="form.warranty" placeholder="请选择保单">
            <el-option
              v-for="dict in dict.type.warranty_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="社保" prop="insurance">
          <el-select v-model="form.insurance" placeholder="请选择社保">
            <el-option
              v-for="dict in dict.type.insurance_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公积金" prop="fund">
          <el-select v-model="form.fund" placeholder="请选择公积金">
            <el-option
              v-for="dict in dict.type.fund_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工资" prop="salary">
          <el-input v-model="form.salary" placeholder="请输入工资"/>
        </el-form-item>
        <el-form-item label="申请额度" prop="applyMoney">
          <el-select v-model="form.applyMoney" placeholder="请选择申请额度">
            <el-option
              v-for="dict in dict.type.quota_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-select v-model="form.province" placeholder="请选择省份" @clear="clearSelect('1','f')" clearable>
            <el-option
              @click.native="getChildren(p.code,'1','f')"
              v-for="p in this.provinceList"
              :key="p.code"
              :label="p.name"
              :value="p.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-select v-model="form.city" placeholder="请选择城市" @clear="clearSelect('2','f')" clearable>
            <el-option
              @click.native="getChildren(p.code,'2','f')"
              v-for="p in this.formCityList"
              :key="p.code"
              :label="p.name"
              :value="p.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="区县" prop="county">
          <el-select v-model="form.county" placeholder="请选择区县" clearable>
            <el-option
              v-for="p in this.formCountyList"
              :key="p.code"
              :label="p.name"
              :value="p.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="bz">
          <el-input v-model="form.bz" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="客户经理" prop="manager">
          <el-input v-model="form.manager" placeholder="请输入客户经理"/>
        </el-form-item>
        <!--        <el-form-item label="客户状态" prop="customerStatus">-->
        <!--          <el-select v-model="form.customerStatus" placeholder="请选择客户的状态">-->
        <!--            <el-option-->
        <!--              v-for="dict in dict.type.customer_status"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.label"-->
        <!--              :value="dict.value"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 分配客户信息 -->
    <bath-dis-customer ref="bathDisCustomer"></bath-dis-customer>
    <!-- 拉取客户信息 -->
    <pull-superior-customer ref="pullSuperiorCustomer"></pull-superior-customer>
  </div>
</template>

<script>
import {
  getCustomer,
  delCustomer,
  addCustomer,
  updateCustomer,
  loadParentOrg
} from "@/api/customer/customer";
import PullSuperiorCustomer from "@/views/components/PullSuperiorCustomer.vue";
import {
  sameLevelPoolList,
  getUserList,
  getDeptList,
  getDeptInfoList,
  returnCustomer
} from "@/api/customerPool/customerPool"
import BathDisCustomer from './BathDisCustomer.vue'
import {left} from "core-js/internals/array-reduce";

export default {
  name: "Customer",
  components: {
    BathDisCustomer,
    PullSuperiorCustomer
  },
  dicts: ['fund_type', 'customer_status', 'sex_type', 'car_type', 'insurance_type', 'house_type', 'warranty_type',
    'level_type', 'loan_type', 'quota_type'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户信息表格数据
      customerList: [],
      // 用户信息数据
      userList: [],
      // 部门信息数据
      deptList: [],
      // 顶级部门信息
      topDeptList: [],
      // 所有部门信息
      allDeptList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分配弹出框
      allotOpen: false,
      // 省级地址
      provinceList: [],
      // 市区地址
      searchCityList: [],
      // 区县
      searchCountyList: [],
      // 市区地址
      formCityList: [],
      // 区县
      formCountyList: [],
      isEdit: false,
      customerIds: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        name: undefined,
        tel: undefined,
        level: undefined,
        loanType: undefined,
        age: undefined,
        sex: undefined,
        label: undefined,
        education: undefined,
        house: undefined,
        car: undefined,
        warranty: undefined,
        insurance: undefined,
        fund: undefined,
        salary: undefined,
        applyMoney: undefined,
        province: undefined,
        city: undefined,
        county: undefined,
        manager: undefined,
        customerStatus: undefined,
        companyStatus: undefined,
        distributeDeptId: undefined,
        share: undefined,
        source: undefined,
        dataSource: undefined,
        vip: undefined,
        taskId: undefined,
        createDate: undefined,
        bz: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "姓名不能为空", trigger: "blur"}
        ],
        tel: [
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确格式的手机号码",
            trigger: "blur"
          }
        ],
        distributeDeptId: [
          {required: true, message: "部门不能为空", trigger: "blur"}
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDeptInfoInit('0');
    this.getDeptInfoInit('1');
    this.loadPOrg('0', 0, 's')
  },
  methods: {
    left,
    /** 获取一级部门信息 */
    getDeptInfoInit(str) {
      if (str === '0') {
        const query = {
          parentId: 0
        }
        getDeptInfoList(query).then(response => {
          this.topDeptList = response.data;
        });
      } else {
        getDeptInfoList().then(response => {
          this.allDeptList = response.data;
        });
      }
    },
    /** 退回 */
    customerReturn(row) {
      const ids = row.id || this.ids
      this.buttonLoading = true;
      const customerIds = [];
      const obj = this.customerList.find(it => it.id = ids)
      const data = {
        id: obj.id,
        deptId: obj.distributeDeptId
      }
      customerIds.push(data);
      this.$confirm('是否确定退回?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return returnCustomer({"customers": customerIds});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("退回成功");
      }).catch(() => {
      })
    },
    /** 处理分配数据 */
    bathDistributionCustomer(row) {
      // todo 格式待处理
      const ids = row.id || this.ids
      this.getUserInfoList();
      this.getDeptInfoList();
      const customers = [];
      customers.push(ids)
      this.$refs.bathDisCustomer.openDialog(customers, 'poolCustomer');
    },
    /** 获取用户信息 */
    getUserInfoList() {
      const query = {
        type: "1"
      }
      getUserList(query).then(response => {
        this.userList = response.data;
      })
    },
    getDeptInfoList() {
      const query = {
        type: "1"
      }
      getDeptList(query).then(response => {
        this.deptList = response.data;
      })
    },
    /** 加载组织 **/
    loadPOrg(parentId, level, type) {
      loadParentOrg(parentId).then(response => {
        if (level == '0') {
          this.provinceList = response.data
        } else if (level == '1') {
          if (type === 's') {
            this.searchCityList = response.data
          } else {
            this.formCityList = response.data
          }
        } else {
          if (type == 's') {
            this.searchCountyList = response.data
          } else {
            this.formCountyList = response.data
          }
        }
      })
    },
    getChildren(parentId, level, type) {
      this.clearSelect(level, type)
      this.loadPOrg(parentId, level, type)
    },

    clearSelect(level, type) {
      if (type == 's') {
        if (level == '1') {
          this.searchCityList = []
          this.queryParams.city = ''
        }
        this.queryParams.county = ''
        this.searchCountyList = []
      } else {
        if (level == '1') {
          this.formCityList = []
          this.form.city = ''
        }
        this.form.county = ''
        this.formCountyList = []
      }
    },
    /** 查询客户信息列表 */
    getList() {
      this.loading = true;
      sameLevelPoolList(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        tel: undefined,
        level: undefined,
        loanType: undefined,
        age: undefined,
        sex: undefined,
        label: undefined,
        education: undefined,
        house: undefined,
        car: undefined,
        warranty: undefined,
        insurance: undefined,
        fund: undefined,
        salary: undefined,
        applyMoney: undefined,
        province: undefined,
        city: undefined,
        county: undefined,
        remark: undefined,
        manager: undefined,
        customerStatus: undefined,
        companyStatus: undefined,
        distributeDeptId: undefined,
        share: undefined,
        source: undefined,
        dataSource: undefined,
        vip: undefined,
        taskId: undefined,
        createBy: undefined,
        createDate: undefined,
        updateBy: undefined,
        updateTime: undefined,
        bz: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      if (this.form.vip) {
        this.form.vip = undefined;
      }
      this.searchCityList = [];
      this.searchCountyList = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 拉取数据 */
    handlePull() {
      this.$refs.pullSuperiorCustomer.openDialog('deptPull');
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log('row:', row)
      this.loading = true;
      this.isEdit = true;
      this.reset();
      const id = row.id || this.ids
      getCustomer(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        // 回选区域信息
        if (this.form.province) {
          this.loadPOrg(this.form.province, 1, "f");
        } else {
          this.searchCityList = [];
        }
        if (this.form.city) {
          this.loadPOrg(this.form.city, 2, "f");
        } else {
          this.searchCountyList = [];
        }
        this.title = "修改客户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addCustomer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delCustomer(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/customer/export', {
        ...this.queryParams
      }, `customer_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.messageCard {
  overflow-y: scroll;
}

.messageCard::-webkit-scrollbar {
  width: 4px;
}

.messageCard::-webkit-scrollbar-thumb {
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.2);
}

.messageCard::-webkit-scrollbar-track {
  border-radius: 0;
  background: rgba(0, 0, 0, 0.1);
}
</style>
